package com.example.myapplication;

import android.app.SharedElementCallback;
import android.content.Context;
import android.graphics.Matrix;
import android.graphics.RectF;
import android.os.Bundle;
import android.os.Parcelable;
import android.service.autofill.ImageTransformation;
import android.transition.ChangeBounds;
import android.transition.ChangeClipBounds;
import android.transition.ChangeImageTransform;
import android.transition.ChangeTransform;
import android.transition.Fade;
import android.transition.Transition;
import android.transition.TransitionSet;
import android.view.View;
import android.view.Window;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;

import com.example.myapplication.adapter.CustomPageAdapter;
import com.example.myapplication.adapter.ImageDetailAdapter;
import com.gyf.immersionbar.ImmersionBar;

import java.util.List;
import java.util.Map;

public class ImageDetailActivity extends AppCompatActivity {

    public static final String EXTRA_DATA = "extra_data";

    private ViewPager2 mViewPager;

    private ImageDetailAdapter mImageDetailAdapter;
    private CustomPageAdapter mCustomPageAdapter;
    private DataConfig mDataConfig;
    private int mCurrentPosition = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
        super.onCreate(savedInstanceState);
        ImmersionBar.with(this).init();
        setContentView(R.layout.activity_detail_custom);

        setEnterSharedElementCallback(new SharedElementCallback() {
            @Override
            public void onSharedElementStart(List<String> sharedElementNames, List<View> sharedElements, List<View> sharedElementSnapshots) {
                super.onSharedElementStart(sharedElementNames, sharedElements, sharedElementSnapshots);
//                TransitionSet transitionSet = new TransitionSet();
//
//                transitionSet.addTransition(new ChangeClipBounds());
//                transitionSet.addTransition(new ChangeTransform());
//                transitionSet.addTransition(new ChangeBounds());
//                transitionSet.addTransition(new ChangeImageTransform());
////                transitionSet.addTransition(new ChangeOnlineImageTransition());
//
//                getWindow().setSharedElementEnterTransition(transitionSet);
//                getWindow().setSharedElementExitTransition(transitionSet);
                getWindow().setEnterTransition(new Fade());
                getWindow().setExitTransition(new Fade());
                //防止状态栏闪烁
                Transition enterTransition = getWindow().getEnterTransition();
                Transition exitTransition = getWindow().getExitTransition();
                if (enterTransition != null) {
                    enterTransition.excludeTarget(Window.STATUS_BAR_BACKGROUND_TRANSITION_NAME, true);
                    enterTransition.excludeTarget(Window.NAVIGATION_BAR_BACKGROUND_TRANSITION_NAME, true);
                }
                if (exitTransition != null) {
                    exitTransition.excludeTarget(Window.STATUS_BAR_BACKGROUND_TRANSITION_NAME, true);
                    exitTransition.excludeTarget(Window.NAVIGATION_BAR_BACKGROUND_TRANSITION_NAME, true);
                }
            }

            @Override
            public void onSharedElementEnd(List<String> sharedElementNames, List<View> sharedElements, List<View> sharedElementSnapshots) {
                super.onSharedElementEnd(sharedElementNames, sharedElements, sharedElementSnapshots);
            }

            @Override
            public void onRejectSharedElements(List<View> rejectedSharedElements) {
                super.onRejectSharedElements(rejectedSharedElements);
            }

            @Override
            public void onMapSharedElements(List<String> names, Map<String, View> sharedElements) {
                super.onMapSharedElements(names, sharedElements);
            }

            @Override
            public Parcelable onCaptureSharedElementSnapshot(View sharedElement, Matrix viewToGlobalMatrix, RectF screenBounds) {
                return super.onCaptureSharedElementSnapshot(sharedElement, viewToGlobalMatrix, screenBounds);
            }

            @Override
            public View onCreateSnapshotView(Context context, Parcelable snapshot) {
                return super.onCreateSnapshotView(context, snapshot);
            }

            @Override
            public void onSharedElementsArrived(List<String> sharedElementNames, List<View> sharedElements, OnSharedElementsReadyListener listener) {
                super.onSharedElementsArrived(sharedElementNames, sharedElements, listener);
            }
        });
        if (getIntent() != null) {
            mDataConfig = (DataConfig) getIntent().getSerializableExtra(EXTRA_DATA);
        }
        if (mDataConfig == null || mDataConfig.getList() == null || mDataConfig.getList().size() == 0) {
            return;
        }
        mCurrentPosition = mDataConfig.getCurrentPosition();
        if (mCurrentPosition < 0) {
            mCurrentPosition = 0;
        } else if (mCurrentPosition >= mDataConfig.getList().size()) {
            mCurrentPosition = mDataConfig.getList().size() - 1;
        }

        supportPostponeEnterTransition();

        mViewPager = findViewById(R.id.view_pager);
        mImageDetailAdapter = new ImageDetailAdapter(this, mDataConfig.getList(), mCurrentPosition);
//        mCustomPageAdapter = new CustomPageAdapter(this);
//        mCustomPageAdapter.setImages(mDataConfig.getList());
        mViewPager.setOffscreenPageLimit(1);
        mViewPager.setAdapter(mImageDetailAdapter);
        mViewPager.setCurrentItem(mCurrentPosition, false);
    }
}